<template>
    <el-dialog
            :title="!dataForm.orderId ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
                            <el-form-item label="需求申请ID" prop="requestId">
                        <el-input v-model.number="dataForm.requestId" placeholder="需求申请ID"></el-input>
                </el-form-item>
                <el-form-item label="申请部门" prop="department">
                        <el-input v-model="dataForm.department" placeholder="申请部门"></el-input>
                </el-form-item>
                <el-form-item label="申请人" prop="applicant">
                        <el-input v-model="dataForm.applicant" placeholder="申请人"></el-input>
                </el-form-item>
                <el-form-item label="物资名称" prop="materialName">
                        <el-input v-model="dataForm.materialName" placeholder="物资名称"></el-input>
                </el-form-item>
                <el-form-item label="数量" prop="quantity">
                        <el-input v-model.number="dataForm.quantity" placeholder="数量"></el-input>
                </el-form-item>
                <el-form-item label="预计使用时间" prop="expectedUseDate">
                        <el-date-picker
                                v-model="dataForm.expectedUseDate"
                                type="datetime"
                                placeholder="预计使用时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="审批状态" prop="status">
                        <el-input v-model="dataForm.status" placeholder="审批状态"></el-input>
                </el-form-item>
                <el-form-item label="审批日期" prop="approvalDate">
                        <el-date-picker
                                v-model="dataForm.approvalDate"
                                type="datetime"
                                placeholder="审批日期">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="审批人" prop="approver">
                        <el-input v-model="dataForm.approver" placeholder="审批人"></el-input>
                </el-form-item>
                <el-form-item label="创建时间" prop="createdAt">
                        <el-date-picker
                                v-model="dataForm.createdAt"
                                type="datetime"
                                placeholder="创建时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="更新时间" prop="updatedAt">
                        <el-date-picker
                                v-model="dataForm.updatedAt"
                                type="datetime"
                                placeholder="更新时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="需求原因" prop="reason">
                        <el-input v-model="dataForm.reason" placeholder="需求原因"></el-input>
                </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
        data () {
            return {
                visible: false,
                dataForm: {
                    requestId: null,
                    department: null,
                    applicant: null,
                    materialName: null,
                    quantity: null,
                    expectedUseDate: null,
                    status: null,
                    approvalDate: null,
                    approver: null,
                    createdAt: null,
                    updatedAt: null,
                    reason: null,
                },
                dataRule: {
                    requestId: [
                        { required: true, message: '需求申请ID不能为空', trigger: 'blur' }
                    ],
                    department: [
                        { required: true, message: '申请部门不能为空', trigger: 'blur' }
                    ],
                    applicant: [
                        { required: true, message: '申请人不能为空', trigger: 'blur' }
                    ],
                    materialName: [
                        { required: true, message: '物资名称不能为空', trigger: 'blur' }
                    ],
                    quantity: [
                        { required: true, message: '数量不能为空', trigger: 'blur' }
                    ],
                    expectedUseDate: [
                        { required: true, message: '预计使用时间不能为空', trigger: 'blur' }
                    ],
                    status: [
                        { required: true, message: '审批状态不能为空', trigger: 'blur' }
                    ],
                    approvalDate: [
                        { required: true, message: '审批日期不能为空', trigger: 'blur' }
                    ],
                    approver: [
                        { required: true, message: '审批人不能为空', trigger: 'blur' }
                    ],
                    createdAt: [
                        { required: true, message: '创建时间不能为空', trigger: 'blur' }
                    ],
                    updatedAt: [
                        { required: true, message: '更新时间不能为空', trigger: 'blur' }
                    ],
                    reason: [
                        { required: true, message: '需求原因不能为空', trigger: 'blur' }
                    ],
                }
            }
        },
        methods: {
            init (id) {
                this.dataForm.orderId = id || 0
                this.visible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                    this.dataForm.orderId = id || 0
                    if (this.dataForm.orderId) {
                        this.$http({
                            url: this.$http.adornUrl(`/app/AppPurchaseRequest/info/\\$\\{this.dataForm.orderId}`),
                            method: 'get',
                            params: this.$http.adornParams()
                        }).then(({data}) => {
                            if (data && data.code === 0) {
                                this.dataForm = data.apppurchaserequest
                            }
                        })
                    }
                })
            },
            // 表单提交
            dataFormSubmit () {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        this.$http({
                            url: this.$http.adornUrl(`/app/AppPurchaseRequest/\\$\\{!this.dataForm.orderId ? 'save' : 'update'}`),
                            method: 'post',
                            data: this.$http.adornData(this.dataForm)
                        }).then(({data}) => {
                            if (data && data.code === 0) {
                                this.$message({
                                    message: '操作成功',
                                    type: 'success',
                                    duration: 1500,
                                    onClose: () => {
                                        this.visible = false
                                        this.$emit('refreshDataList')
                                    }
                                })
                            } else {
                                this.$message.error(data.msg)
                            }
                        })
                    }
                })
            }
        }
    }
</script>